package com.sms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sms.service.BisWechatRecordService;
import com.system.common.pojo.BisCustomUser;


@Controller
@RequestMapping("sms")
public class BisWechatRecordController {
  @Autowired
  private BisWechatRecordService bisWechatRecordService;
  @RequestMapping("wechatshow")
  public String list(Model model,HttpServletRequest request){
	  Map<String,Object> map=initData(request);
	   List<Map<String,Object>> list=bisWechatRecordService.selectAll(map);
	   model.addAttribute("list", list);
	   Map<String,Object> map1=new HashMap<>();
	   List<BisCustomUser> cList=bisWechatRecordService.queryBisCustomUser(map1);
	   model.addAttribute("cList", cList);
	  return "sms/wechatList";
  }
  @RequestMapping("userList")
  @ResponseBody
  public void userList(String qusername,String qmobile,HttpServletResponse response){
	  Map<String,Object> map=new HashMap<>();
	  map.put("qmobile", qmobile);
	  map.put("qusername", qusername); 
	  List<BisCustomUser> cList=bisWechatRecordService.queryBisCustomUser(map);
	  try {
		ObjectMapper mapper=new ObjectMapper(); 
		String json=mapper.writeValueAsString(cList);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
  public Map<String, Object> initData(HttpServletRequest request){
	   Map<String,Object> map=new HashMap<>(); 
	   String qusername=request.getParameter("qusername");
	   if (qusername!=null&&!qusername.equals("")) {
		map.put("qusername", qusername); 
		request.setAttribute("qusername", qusername);
	}
	   String qmobile=request.getParameter("qmobile");
	   if (qmobile!=null&&!qmobile.equals("")) {
		map.put("qmobile", qmobile); 
		request.setAttribute("qmobile", qmobile);
	}
	   String qcontent=request.getParameter("qcontent");
	   if (qcontent!=null&&!qcontent.equals("")) {
		map.put("qcontent", qcontent); 
		request.setAttribute("qcontent", qcontent);
	}
	   String qsend=request.getParameter("qsend");
	   if (qsend!=null&&!qsend.equals("")) {
		map.put("qsend", qsend); 
		request.setAttribute("qsend", qsend);
	}
	   String qend=request.getParameter("qend");
	   if (qend!=null&&!qend.equals("")) {
		map.put("qend", qend); 
		request.setAttribute("qend", qend);
	}
	   return map;
  }
  
  @RequestMapping("wecahtAdd")
  public String wecahtAdd(HttpServletRequest request){
		 List<String> openid=new ArrayList<>();
		 String customUserId=request.getParameter("customUserId");
		 //System.out.println("id:"+customUserId);
		 String content=request.getParameter("content");
		 String userId=request.getParameter("userId");
		 String[] sendids=customUserId.split(",");
		 for (int i = 0; i < sendids.length; i++) {
			 openid.add(sendids[i]);
		}
		 String[] userIds=userId.split(",");
		 for (int i = 0; i < userIds.length; i++) {
			 //System.out.println(userIds[i]);
			 this.bisWechatRecordService.saveBisWechatRecord(openid, content,userIds[i]);
		}
	  return "redirect:/sms/wechatshow";
  }
  @RequestMapping("deleteWechat")
  public String del(String recordId){
	  bisWechatRecordService.delBisWechatRecord(recordId);
	  return "redirect:/sms/wechatshow";
  }
}
